<template>
	<view>
		<!-- 当前登录人是管理端 -->
		<manage-home v-if="role!='0'"></manage-home>
		<!-- 当前登录人是普通客户 -->
		<view v-if="role=='0'" :style="{marginBottom:marginBottom}">
			<view class="view-top">
				<view style="height: 12rpx;width: 100%;background-color: white;"></view>
				<swiper indicator-dots autoplay :interval="3000" :duration="500" class="swiper" indicator-color="#444444"
				 indicator-active-color="#ffffff">
					<block v-for="(item,index) in ads" :key="index">
						<swiper-item @click="adsClick(item)">
							<view class="swiper-item">
								<image :src="item.picUrls" mode="scaleToFill" style="height: 274rpx;width: 100%;"></image>
							</view>
						</swiper-item>
					</block>
				</swiper>
				<view class="view-top-under">
					
					<image :src="phone_buy" class="view-top-under-l" @click="goBuy"></image>
					<image :src="phone_fq" class="view-top-under-r" @click="goInitiated"></image>
				</view>
			</view>
			
			<!-- 列表 -->
			
			<view style="display: flex;align-items: center;margin-top: 32rpx;">
				<text class="center-text">案件列表</text>
			</view>
			<case-list :caseListData="caseListData" v-if="isPhone && caseListData.length>0" :role="role"></case-list>
			
			
			<view class="" v-if="!isPhone || caseListData.length==0">
				<view class="desc1" style="margin-top: 40rpx;">
					<image :src="no_case" style="width: 300rpx;height: 300rpx;"></image>
				</view>
				<view class="desc1">
					<text class="desc11">暂无案件</text>
				</view>
				<view class="desc1" style="margin-top: 16rpx;" v-if="!isPhone">
					<text class="desc12">绑定手机完成注册后，可发起咨询，参与案件交流~</text>
				</view>
				<view class="desc1" style="margin-top: 24rpx;">
					<button 
					v-if="!isPhone && isAuth"
					hover-class='none'
							withCredentials="true" 
							class="userphone"
							 open-type="getPhoneNumber" @getphonenumber="getphonenumber">
							绑定手机
						
							</button>
							
							<button
							v-if="!isAuth"
							hover-class='none'
									withCredentials="true" 
									class="userphone"
									@click="getUserProfile"
									>
									微信授权
								
									</button>
				</view>
			</view>
			
		</view>
		
		<view class="zhezhao" v-if="!isAuth && showZhezhao">
			<view style="width: 622rpx;height: 540rpx;">
				<image :src="no_auth" style="width: 622rpx;height: 540rpx;"></image>
			</view>
			
			<view style="width: 622rpx;height: 540rpx;margin-left: -622rpx;">
				<view style="display: flex;align-items: center;">
					<view class="colsezhezhao" @click="colseZhezhao">
						<image src="../../../../static/close2.png" style="height:24rpx;width: 24rpx;"></image>
					</view>
				</view>
				<button
					hover-class='none' 
					withCredentials="true"
					class="user-auth"
					 @click="getUserProfile">
					微信授权
				</button>
			</view>
		</view>
		
		<tab-bar :selected="0"  ref='tabBar'></tab-bar>
	</view>
</template>

<script>
	import tabBar from '../../common/tab-bar.vue'
	import manageHome from '../../manage/home/home.vue'
	import caseList from '../../common/caseList.vue'
	let app = ""
	export default{
		components:{tabBar,manageHome,caseList},
		data(){
			return{
				marginBottom:'',
				role:'0',
				ads:[],
				caseListData:[],
				
				isAuth:true,//授权登录标识
				isPhone:true,//授权手机号标识
				
				showZhezhao:false,
				sessionKey:'',
				no_auth:getApp().globalData.baseImageUrl+'/no_auth.png',
				no_case:getApp().globalData.baseImageUrl+'/no_case.png',
				phone_fq:getApp().globalData.baseImageUrl+'/phone_fq.png',
				phone_buy:getApp().globalData.baseImageUrl+'/phone_buy.png',
			}
		},
		onLoad() {
			let _this = this
			app = getApp()
			
			console.log('load',_this.role)
			console.log(app.globalData.platform)
			let platform = app.globalData.platform
			if(platform=='windows' || platform =='mac'){

				//|| platform =='devtools'
				console.log('跳转')
				// uni.reLaunch({
				// 	url:'/packageC/PhonePC/caseList/caseDetail/caseDetail'
				// })
				uni.reLaunch({
					url:'/packageC/PhonePC/index'
				})
			
			}else{

				_this.needAdapt = app.globalData.needAdapt && app.globalData.isIPhone
				if (_this.needAdapt) {
					_this.marginBottom = 180 + 'rpx'
			
				} else {
					_this.marginBottom = 110 + "rpx"
				
				}
				// _this.login()
				
				//微信登录相关
				_this.wxLogin()
			}
			
		},
		onShow() {
			this.isAuth = app.globalData.isAuth
			this.isPhone = app.globalData.isPhone
			if(!this.isAuth){
				this.showZhezhao = true
			}
			let initated = uni.getStorageSync("initated")
			if(initated!=null && initated!=undefined){
				this.caseListData = []
				this.getCaseList()
				uni.setStorageSync("initated",null)
			}
		},
	
		methods:{
			wxLogin(){
				uni.showLoading({
					title:'正在登录...'
				})
				let _this = this
				uni.login({
					success: (res) => {
						console.log('获取code',res)
						_this.getLoginCode(res.code)
						
					},
					fail: (err) => {
						uni.hideLoading()
						uni.showToast({
							title:'登录失败，请退出重试！',
						})
					}
				})
				
			},
			async getLoginCode(code){
				let _this = this
				const res = await _this.$myRequest({
					url:`/wx/ma/user/${app.globalData.appidKey}/login`,
					data:{
						code:code
					},
					header:{
						"Content-Type":"application/x-www-form-urlencoded",
						"Authorization":"Basic ZG90Z286ZG90Z28="
					},
					
				})
				console.log('获取loginCode',res)
				if(res.data.code==200){
					_this.sessionKey = res.data.data.sessionKey
					app.globalData.sessionKey = res.data.data.sessionKey
					_this.codeLogin(res.data.data)
				}else{
					uni.hideLoading()
					uni.showToast({
						title:'获取登录code失败，请退出重试！',
					})
				}
			},
			
			async codeLogin(data){
				let _this = this
				const res = await _this.$myRequest({
					url:`/oauth/login/token/maCode`,
					header:{
						"Content-Type":"application/x-www-form-urlencoded",
						"Authorization":"Basic ZG90Z286ZG90Z28=",
						// "from":"sys"
					},
					method:'POST',//默认是GET
					data:{
						loginCode:data.loginCode,
						appidkey:app.globalData.appidKey
					}
				})
				uni.hideLoading()
				console.log('code登录',res)
				if(res.data.code==200){
					// _this.codeLogin(res.data.data)
					uni.setStorageSync("accessToken",res.data.data.accessToken)
					uni.setStorageSync("refreshToken",res.data.data.refreshToken)
					
					_this.getUser()
				}else{
					
					uni.showToast({
						title:'登录失败，请退出重试！',
					})
				}
			},

				
			async login(){
				let _this = this
				let parmas = {
					"username":'admin',
					"password":"123456",
					
				}
				const res = await _this.$myRequest({
					url: "/oauth/login/token/passwd",
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"Authorization":"Basic ZG90Z286ZG90Z28=",
						 "from":"sys"
					},
					data: parmas,
					method:'POST',//默认是GET
				})
				// console.log('返回结果',res)
				if(res.data.code==200){
					uni.setStorageSync("accessToken",res.data.data.accessToken)
					uni.setStorageSync("refreshToken",res.data.data.refreshToken)
					_this.getUser()
				}
				
				
			},
			
			async getUser(){
				let _this = this
			
				const res = await _this.$myRequest({
					url: "/oauth/userinfo",
					// header: {
					// 	"Authorization":"Basic ZG90Z286ZG90Z28=",
					// 	"token":uni.getStorageSync("accessToken")
					// },
				})
	
				console.log('返回用户信息结果',res)
				if(res.data.code==200){
					
					app.globalData.role = res.data.data.userType.code
					//此 实际情况以登录返回为准
					_this.role = app.globalData.role
					
					_this.$refs.tabBar.initRole()
					
			
					// let avatarPic = res.data.data.avatarPic
					// if(avatarPic==null || avatarPic== '' || avatarPic==undefined){
					// 	_this.isAuth = false
					// 	app.globalData.isAuth = false
					// }else{
					// 	app.globalData.userInfo = res.data.data
					// 	app.globalData.isAuth = true
					// 	_this.isAuth = true
					// }
					
					
					let mobile = res.data.data.mobile
					
					console.log('mobile',mobile)
					app.globalData.userInfo = res.data.data
					if(mobile==null || mobile== '' || mobile==undefined || mobile.length<11){
						_this.isPhone = false
						app.globalData.isPhone = false
					}else{
						app.globalData.userInfo = res.data.data
						app.globalData.userInfo.mobile = mobile
						app.globalData.isPhone = true
						_this.isPhone = true
						
					}
					app.globalData.userInfo = res.data.data
					
					//普通用户  调用普通用户的接口
					if(_this.role == '0'){
						_this.getAdv()
						if(_this.isPhone){
							_this.getCaseList()
						}					
					}		
				}
			},
			
			/**
			 * 获取案件列表
			 */
			async getCaseList(){
				let _this = this
				let parmas={
					"page":1,
					"size":1000,
				}
				const res = await _this.$myRequest({
					url: "/client/case/list",
					data: parmas,
					method:'POST',//默认是GET
				})
				// uni.hideLoading()
				console.log('案件列表',res)
				if(res.data.code==200){
					_this.caseListData = res.data.data.list
				}
			},
			//获取广告
			async getAdv(){
				let _this = this
				const res = await _this.$myRequest({
					url: "/client/advert/list",
					method:'POST',//默认是GET
				})
			
				console.log('广告位',res)
				if(res.data.code==200){
					_this.ads = []
					let list = res.data.data.list
					
					for (var i = 0; i < list.length; i++) {
						
						if( list[i]['picUrls']!='' && list[i]['picUrls']!=undefined){
							list[i]['picUrls'] = _this.$imageUrls({
								picUrls: list[i]['picUrls'] ,
								height:200
							})
							_this.ads.push(list[i])
						}

					}
					
				}
			},
			
			colseZhezhao(){
				this.showZhezhao = false
			},
			/**
			 * 点击了授权
			 */
			getUserProfile(){
				
				let _this = this
				// _this.showZhezhao = false
				uni.getUserProfile({
					desc:'完善用户资料',
					success(res) {
						console.log(res)
						_this.getWxInfo(res)
						// _this.isAuth = true
						// app.globalData.isAuth = true
						// _this.userInfo = res.userInfo
						// _this.headImage = res.userInfo.avatarUrl
						// _this.userName = res.userInfo.nickName
					}
				})
			},
			async getWxInfo(data){
				let _this = this
				uni.showLoading({
					
				})
				const res = await _this.$myRequest({
					url:`/wx/ma/user/${app.globalData.appidKey}/info`,
					data:{
						sessionKey:_this.sessionKey,
						signature:data.signature,
						rawData:data.rawData,
						encryptedData:data.encryptedData,
						iv:data.iv
					}
				})
				uni.hideLoading()
				console.log('获取info',res)
				if(res.data.code==200){
					_this.getUser()
					// _this.showZhezhao = false
					// _this.isAuth = true
					// app.globalData.isAuth = true
					// _this.userInfo = res.data.data
					// app.globalData.userInfo = res.data.data
				}
			},
			
			/**
			 * 点击了获取手机号
			 */
			getphonenumber(e){
				console.log(e)
				let code = e.detail.code
				let encryptedData = e.detail.encryptedData
				let errMsg = e.detail.errMsg
				let iv = e.detail.iv
				
				let _this = this
				
				_this.getWxPhone(encryptedData,iv,code)
				
				

			},
			async getWxPhone(data1,data2,data3){
				let _this = this
				uni.showLoading({
					
				})
				const res = await _this.$myRequest({
					url:`/wx/ma/user/${app.globalData.appidKey}/phone`,
					data:{
						sessionKey:_this.sessionKey,
						encryptedData:data1,
						iv:data2,
						code:data3
					}
				})
				uni.hideLoading()
				console.log('获取手机号',res)
				if(res.data.code==200){
					app.globalData.userInfo.mobile = res.data.data.phoneNumber
					_this.isPhone = true
					app.globalData.isPhone = true
				}
				
				
			},
			/**
			 * 点击 发起咨询
			 */
			goInitiated(){
				console.log('发起')
				if(!this.isPhone){
					uni.showModal({
						title:'提示',
						content:'请先绑定手机号',
						showCancel:false,
					})
				}else{
					
					//先查询剩余次数
					this.customerRights_check()
							
					
				}
				
			},
			
			/**
			 * 查询剩余咨询次数
			 */
			async customerRights_check(){
				let _this = this
				uni.showLoading({
					title:'正在查询...'
				})
				const res = await _this.$myRequest({
					url: `/client//case/check`,
				})
				uni.hideLoading()
				console.log('查询次数',res)
				if(res.data.code==200){
					let list = res.data.data
					let size = list.length
					if(size===0){
						uni.showModal({
							title:'提示',
							content:'未查询到当前有可咨询的次数，请先购买咨询服务吧~',
							success: (res) => {
								if(res.confirm){
									uni.navigateTo({
										url:'../buyServe/buyServe'
									})
								}
							}
						})
					}else{
						uni.navigateTo({
							url:'../initiatedConsult/initiatedConsult?from=add'
						})
					}
				}
			},
			
			
			
			
			
			
			/**
			 * 点击购买服务
			 */
			goBuy(){
				console.log('购买服务')
				if(!this.isPhone){
					uni.showModal({
						title:'提示',
						content:'请先绑定手机号',
						showCancel:false,
					})
				}else{
					uni.navigateTo({
						url:'../buyServe/buyServe'
					})
				}
				
			},
			
			/**
			 * 广告位点击
			 */
			adsClick(val){
				console.log(val)
				if(val.targetType.code=="3"){
					let targetUrl = val.targetUrl
					uni.navigateTo({
						url:'/pages/phone/customs/webview/webView?targetUrl='+targetUrl
					})
				}else if(val.targetType.code=="2"){
					if(!this.isPhone){
						uni.showModal({
							title:'提示',
							content:'请先绑定手机号',
							showCancel:false,
						})
					}else{
						let id = val.targetUrl
						uni.navigateTo({
							url:'../serveDetail/serveDetail?id='+id
						})
					}
					
				}else if(val.targetType.code=="1"){
					if(!this.isPhone){
						uni.showModal({
							title:'提示',
							content:'请先绑定手机号',
							showCancel:false,
						})
					}else{
						uni.navigateTo({
							url:'../buyServe/buyServe'
						})
					}
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.view-top{
		width: 100%;
		height: 430rpx;
		background-color: white;
	}
	.swiper {
		height: 274rpx;
		width: 686rpx;
		margin-left: 32rpx;
	}
	.view-top-under{
		display: flex;
		align-items: center;
		margin-top: 24rpx;
	}
	.view-top-under-l{
		width: 330rpx;
		height: 96rpx;
		margin-left: 32rpx;
	}
	.view-top-under-r{
		width: 330rpx;
		height: 96rpx;
		margin-left: 26rpx;
	}
	.center-text{
		font-size: 36rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #333333;
		line-height: 50rpx;
		margin-left: 32rpx;
	}
	
	.zhezhao {
		width: 100%;
		height: 100vh;
		background: rgba(0, 0, 0, 0.4);
		z-index: 9999;
		position: fixed;
		left: 0;
		top: 0;
		overflow: scroll;	
		// padding-top: 10vh;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.colsezhezhao{
		width: 120rpx;
		height: 120rpx;
		margin-left: auto;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.user-auth{
		width: 400rpx;
		height: 88rpx;
		background: #2DA9C5;
		border-radius: 44rpx;
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 272rpx;
		margin-left: 112rpx;
	}
	
	.desc1{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.desc11{
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #999999;
	}
	.desc12{
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
		
	}
	.userphone{
		width: 260rpx;
		height: 88rpx;
		background: #2DA9C5;
		border-radius: 44rpx;
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	button{
	  /*  line-height: 1;
	    text-align: center;
	   font-size: 32rpx; */
	    padding-left: 0;
	    padding-right: 0;
	    background-color: transparent;
		
	}
	button::after {
		 border: none;
	}
</style>

